﻿using IPluginInterface.Loader;
using Qting.AOI.Serialize;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IPluginInterface
{
    public class OneAoiMesPlugin
    {
        [Category("插件信息")]
        [DisplayName("路径")]
        [Description("路径")]
        public string PluginFileName { get { return sponsor != null ? sponsor.PluginFileName : ""; } }
        [Category("插件信息")]
        [DisplayName("版本")]
        [Description("版本号")]
        public Version Version { get { return plugin != null ? plugin.Version : null; } }
        [Category("插件信息")]
        [DisplayName("标识")]
        [Description("标识")]
        public string Tag { get { return plugin != null ? plugin.Tag : ""; } }
        [Category("插件信息")]
        [DisplayName("说明")]
        [Description("说明")]
        public string Description { get { return plugin != null ? plugin.Description : ""; } }
        [Browsable(false)]

        public bool IsDisposed { get { return sponsor != null ? sponsor.IsDisposed : true; } }
        [Browsable(false)]

        public IAoiPlugin Plugin { get { return plugin; } }
        private PluginHost pluginHost;
        private IAoiPlugin plugin;
        Sponsor<IAoiPlugin> sponsor;
        public OneAoiMesPlugin()
        {

        }
        public OneAoiMesPlugin(Sponsor<IAoiPlugin> sponsor)
        {
            this.sponsor = sponsor;
            this.plugin = sponsor.Instance;
        }

        public void Dispose()
        {
            if (plugin != null) plugin.Dispose();
            if (sponsor != null) sponsor.Dispose();
            pluginHost.Dispose();
        }

        ~OneAoiMesPlugin()
        {
            if (plugin != null) plugin.Dispose();
            if (sponsor != null) sponsor.Dispose();
            if (pluginHost != null) pluginHost.Dispose();
        }

        public PluginResult ExecuteMethod(Method method, ref AoiPcbs aoiPcbs, bool bDual = false, params object[] args)
        {
            PluginResult pluginResult = new PluginResult();
            if (plugin == null) return pluginResult;
            switch (method)
            {
                case Method.Init: pluginResult = plugin.Init(args); break;
                case Method.Start: pluginResult = plugin.Start(ref aoiPcbs, bDual, args); break;
                case Method.BeforeProductIn: pluginResult = plugin.BeforeProductIn(ref aoiPcbs, bDual, args); break;
                case Method.ProductIn: pluginResult = plugin.ProductIn(ref aoiPcbs, bDual, args); break;
                case Method.AfterProductIn: pluginResult = plugin.AfterProductIn(ref aoiPcbs, bDual, args); break;
                case Method.BeforeProductOut: pluginResult = plugin.BeforeProductOut(ref aoiPcbs, bDual, args); break;
                case Method.ProductOut: pluginResult = plugin.ProductOut(ref aoiPcbs, bDual, args); break;
                case Method.AfterProductOut: pluginResult = plugin.AfterProductOut(ref aoiPcbs, bDual, args); break;
                case Method.Done: pluginResult = plugin.Done(ref aoiPcbs, bDual, args); break;
                case Method.Setting: plugin.OpenSettingForm(args); break;
                case Method.CallYiNing: plugin.YiNing(ref aoiPcbs, args); break;
            }
            return pluginResult;
        }
        /// <summary>
        /// 加载插件
        /// </summary>
        /// <param name="pluginFile">插件dll地址</param>
        /// <param name="args">初始化参数</param>
        /// <returns></returns>
        public bool LoadPlugin(string pluginFile, params object[] args)
        {
            bool suc = false;
            string dir = Path.GetDirectoryName(pluginFile);
            string dllName = Path.GetFileName(pluginFile);
            pluginHost = new PluginHost(dir);
            suc = pluginHost.LoadPlugins(dllName);
            if (!suc) return suc;
            sponsor = pluginHost.GetImplementation<IAoiPlugin>();
            if (sponsor == null)
            {
                suc = false;
            }
            else
            {
                this.plugin = sponsor.Instance;
                this.plugin.Init(args);
            }
            return suc;
        }

        public void PluginIni(params object[] args)
        {
            plugin.Init(args);
        }
        public void OpenSettingForm(params object[] args)
        {
            plugin.OpenSettingForm(args);
        }
    }
}
